import {  Guard, IGuard  ,getPropertyMetadata } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { ROLE_META_KEY } from '../decorator/role.decorator';
@Guard()
export class AuthGuard implements IGuard<Context> {
    async canActivate(context: Context, supplierClz, methodName: string): Promise<boolean> {
        // 从类元数据上获取角色信息

        const roleNameList = getPropertyMetadata<string[]>(ROLE_META_KEY, supplierClz, methodName);
        console.log( 'list' , roleNameList);
            console.log( 'role',context.user.role);
            console.log( 'user',context.user);
            
            console.log("methodName",methodName);
            console.log("supplierClz",supplierClz);
            if(!roleNameList){
              return true
            }
  
        
        if (roleNameList && roleNameList.length && context.user.role) {
          // 假设中间件已经拿到了用户角色信息，保存到了 context.user.role 中
          // 直接判断是否包含该角色    
                console.log("context.user.role",context.user.role);
                console.log("c",roleNameList);
                

                return roleNameList.some( v=>{
                   return  context.user.role.includes( v )    
                })
                // return roleNameList.includes( context.user.role)
       
        }
        
        return false;
      }
}